/*
 * @Description: 第三十六章 （开发h5适配）
 * @Author: IA ia@kepro.space
 * @Date: 2025-02-23 02:25:58
 */

import { Plugin } from 'postcss'

const Options = {
  viewportWidth: 375, //UI设计稿的宽度
}

interface Options {
  viewportWidth?: number
}

export const PostcssPxToViewport = (options: Options = Options): Plugin => {
  const opt = Object.assign({}, Options, options)
  return {
    postcssPlugin: 'postcss-px-to-viewport',
    // 钩子函数
    Declaration(node) {
      // console.log('plugin postcss-px-to-viewport node : ', node)
      // console.log('plugin postcss-px-to-viewport node prop value: ', node.prop, ' <==> ', node.value)
      if (node.value.includes('px')) {
        const num = parseFloat(node.value)
        // node.value = `${((num / opt.viewportWidth) * 100).toFixed(2)}vw`
      }
    },
  }
}
